/**
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2009，所有权利保留。
 * 
 * 项目名：	rumba-widget2
 * 文件名：	RSubMenuItem.java
 * 模块说明：	
 * 修改历史：
 * Jan 18, 2009 - lxm - 创建。
 */
package com.hd123.rumba.gwt.widget2.client.menu;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHTML;
import com.google.gwt.user.client.ui.Widget;
import com.hd123.rumba.gwt.widget2.client.misc.RImageLocation;

/**
 * 具有子菜单的菜单项。
 * <p>
 * 
 * <b>CSS Style Rules</b> <ol type=disc>
 * <li>.rb-PopupMenu .rb-SubMenuItem { 整个菜单项，正常状态 }</li>
 * <li>.rb-PopupMenu .rb-SubMenuItem-selected { 整个菜单项，选中状态 }</li>
 * <li>.rb-PopupMenu .rb-SubMenuItem-disabled { 整个菜单项，不可用状态 }</li>
 * <li>.rb-PopupMenu .rb-SubMenuItem .icon { 菜单项的图标 }</li>
 * <li>.rb-PopupMenu .rb-SubMenuItem .seperator { 菜单项的图标与标题之间的分隔线 }</li>
 * <li>.rb-PopupMenu .rb-SubMenuItem .caption { 菜单项的标题 }</li>
 * <li>.rb-PopupMenu .rb-SubMenuItem .arrow { 菜单项的向右箭头，正常状态 }</li>
 * <li>.rb-PopupMenu .rb-SubMenuItem .disabledArrow { 菜单项的向右箭头，不可用状态 }</li> </ol>
 * 
 * @author lxm
 * 
 */
public class RSubMenuItem extends Composite implements HasHTML, IsRMenuItem {

  private static final String STYLENAME_DEFAULT = "rb-SubMenuItem";

  /**
   * 创建对象实例。
   */
  public RSubMenuItem() {
    super();
    drawSelf();
    subMenu = new RPopupMenu();
  }

  public RSubMenuItem(String text) {
    this();
    setText(text);
  }

  private RPopupMenu parentMenu;
  private RPopupMenu subMenu;
  private RImageLocation icon;
  private RImageLocation disabledIcon;
  private boolean enabled = true;
  private Grid grid;
  private HTML htmlCaption;

  public String getHTML() {
    return htmlCaption.getHTML();
  }

  public void setHTML(String s) {
    htmlCaption.setHTML(s);
  }

  public String getText() {
    return htmlCaption.getText();
  }

  public void setText(String s) {
    htmlCaption.setText(s);
  }

  public RPopupMenu getParentMenu() {
    return parentMenu;
  }

  public void setParentMenu(RPopupMenu menu) {
    parentMenu = menu;
    subMenu.setParentMenu(menu);
  }

  public Widget toWidget() {
    return this;
  }

  public boolean isOrHasChild(Element element) {
    return DOM.isOrHasChild(getElement(), element);
  }

  public boolean asSeperator() {
    return false;
  }

  public void onClick() {
    // Do Nothing
  }

  public void onMouseOver() {
    // Do Nothing
  }

  public void onMouseOut() {
    // Do Nothing
  }

  public void setSelectionStyle(boolean selected) {
    if (enabled)
      if (selected)
        addStyleDependentName("selected");
      else
        removeStyleDependentName("selected");
  }

  public RPopupMenu getSubMenu() {
    return subMenu;
  }

  public Widget getContextWidget() {
    return parentMenu == null ? null : parentMenu.getContextWidget();
  }

  public RImageLocation getIcon() {
    return icon;
  }

  public void setIcon(RImageLocation icon) {
    this.icon = icon;
    refreshIcon();
  }

  public RImageLocation getDisabledIcon() {
    return disabledIcon;
  }

  public void setDisabledIcon(RImageLocation disabledIcon) {
    this.disabledIcon = disabledIcon;
    refreshIcon();
  }

  public boolean isEnabled() {
    return enabled;
  }

  public void setEnabled(boolean enabled) {
    this.enabled = enabled;
    refreshEnabled();
  }

  private void drawSelf() {
    grid = new Grid(1, 4);
    grid.setCellPadding(0);
    grid.setCellSpacing(0);
    grid.setWidth("100%");

    grid.setWidget(0, 0, new HTML(""));
    grid.getCellFormatter().setStyleName(0, 0, "icon");

    grid.getCellFormatter().setStyleName(0, 1, "seperator");

    htmlCaption = new HTML("");
    grid.setWidget(0, 2, htmlCaption);
    grid.getCellFormatter().setStyleName(0, 2, "caption");

    grid.setWidget(0, 3, new HTML(""));
    grid.getCellFormatter().setStyleName(0, 3, "arrow");

    initWidget(grid);
    setStyleName(STYLENAME_DEFAULT);
    refreshIcon();
  }

  private void refreshIcon() {
    RImageLocation showIcon = icon;
    if (!enabled)
      showIcon = disabledIcon;
    if (showIcon == null)
      showIcon = RImageLocation.TRANSPARENT_SMALL;
    grid.setWidget(0, 0, showIcon.createImage());
  }

  private void refreshEnabled() {
    if (enabled) {
      DOM.removeElementAttribute(htmlCaption.getElement(), "disabled");
      grid.getCellFormatter().setStyleName(0, 3, "arrow");
    } else {
      DOM.setElementAttribute(htmlCaption.getElement(), "disabled", "false");
      grid.getCellFormatter().setStyleName(0, 3, "disabledArrow");
    }
    refreshIcon();
  }

}
